package com.ruoyi.clerkBalance.service.lmpl;

import com.ruoyi.adapayBalance.domain.AdapayBalance;
import com.ruoyi.adapayBalanceRecord.domain.AdapayBalanceRecord;
import com.ruoyi.clerkBalance.domain.ClerkBalance;
import com.ruoyi.clerkBalance.mapper.ClerkBalanceMapper;
import com.ruoyi.clerkBalance.service.IClerkBalanceService;
import com.ruoyi.clerkBalanceRecord.domain.ClerkBalanceRecord;
import com.ruoyi.clerkBalanceRecord.mapper.ClerkBalanceRecordMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ClerkBalanceServicelmpl implements IClerkBalanceService {

    @Autowired
    private ClerkBalanceMapper clerkBalanceMapper;
    @Autowired
    private ClerkBalanceRecordMapper clerkBalanceRecordMapper;
    /**
     * 查询余额记录
     *
     * @param id 余额记录主键
     * @return 余额记录
     */
    @Override
    public ClerkBalance selectClerkBalanceById(Long id)
    {
        return clerkBalanceMapper.selectClerkBalanceById(id);
    }

    @Override
    public ClerkBalance selectClerkBalanceByStoreUserId(Long storeUserId)
    {
        return clerkBalanceMapper.selectClerkBalanceByStoreUserId(storeUserId);
    }

    /**
     * 查询余额记录列表
     *
     * @param adapayBalance 余额记录
     * @return 余额记录
     */
    @Override
    public List<ClerkBalance> selectClerkBalanceList(ClerkBalance adapayBalance)
    {
        return clerkBalanceMapper.selectClerkBalanceList(adapayBalance);
    }

    /**
     * 新增余额记录
     *
     * @param adapayBalance 余额记录
     * @return 结果
     */
    @Override
    public int insertClerkBalance(ClerkBalance adapayBalance)
    {
        return clerkBalanceMapper.insertClerkBalance(adapayBalance);
    }

    /**
     * 修改余额记录
     *
     * @param adapayBalance 余额记录
     * @return 结果
     */
    @Override
    public int updateClerkBalance(ClerkBalance adapayBalance)
    {
        adapayBalance.setUpdateTime(DateUtils.getNowDate());
        return clerkBalanceMapper.updateClerkBalance(adapayBalance);
    }

    /**
     * 批量删除余额记录
     *
     * @param ids 需要删除的余额记录主键
     * @return 结果
     */
    @Override
    public int deleteClerkBalanceByIds(Long[] ids)
    {
        return clerkBalanceMapper.deleteClerkBalanceByIds(ids);
    }


    /**
     * 批量逻辑删除余额记录
     *
     * @param ids 需要删除的余额记录主键
     * @return 结果
     */
    @Override
    public int logicDeleteClerkBalance(Long[] ids,String delType,Long delUser)
    {
        return clerkBalanceMapper.logicDeleteClerkBalance(ids,delType,delUser);
    }

    /**
     * 删除余额记录信息
     *
     * @param id 余额记录主键
     * @return 结果
     */
    @Override
    public int deleteClerkBalanceById(Long id)
    {
        return clerkBalanceMapper.deleteClerkBalanceById(id);
    }

    public ClerkBalance getClerkBalanceByStoreId(Long storeId){
        ClerkBalance adapayBalanceQ=new ClerkBalance();
        adapayBalanceQ.setStoreId(storeId);
        List<ClerkBalance> balanceList=  selectClerkBalanceList(adapayBalanceQ);
        if(balanceList.size()==1){
            return  balanceList.get(0);
        }else{
            return null ;
        }
    }

    public ClerkBalance getClerkBalanceByStoreUserId(Long storeUserId){
        ClerkBalance adapayBalanceQ=new ClerkBalance();
        adapayBalanceQ.setStoreUserId(storeUserId);
        List<ClerkBalance> balanceList=  selectClerkBalanceList(adapayBalanceQ);
        if(balanceList.size()==1){
            return  balanceList.get(0);
        }else{
            return null ;
        }
    }

    /**
     *
     * @param storeId
     * @param money
     * @param hanleType 加法add 减法sub
     */
    public void handleBalance(Long storeId,Long storeUserId, BigDecimal money, String hanleType, String handleName, String techRemark, String busType,String type){
        if(storeId==null){
            storeId=0L;
        }
        ClerkBalanceRecord clerkBalanceRecord=new ClerkBalanceRecord();
        clerkBalanceRecord.setStoreId(storeId);
        clerkBalanceRecord.setStoreUserId(storeUserId);
        clerkBalanceRecord.setMoney(money);
        clerkBalanceRecord.setHandleType(hanleType);
        clerkBalanceRecord.setName(handleName);
        clerkBalanceRecord.setBusType(busType);
        clerkBalanceRecord.setTechRemark(techRemark);
        clerkBalanceRecord.setCreateTime(DateUtils.getNowDate());
        clerkBalanceRecordMapper.insertClerkBalanceRecord(clerkBalanceRecord);

        Map<String,Object> map=new HashMap<String,Object>();
        map.put("storeId",storeId);
        map.put("money",money);
        map.put("hanleType",hanleType);
        map.put("storeUserId",storeUserId);
        if(type.equals("div")){
            clerkBalanceMapper.handleBalance(map);
        }else if(type.equals("clerksub")){
            clerkBalanceMapper.handleBalance3(map);
        }else{
            clerkBalanceMapper.handleBalance2(map);
        }
    }

}
